<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: #f2f3f5;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
        }
        .card {
            background: #fff;
            padding: 40px 50px;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
            width: 360px;
        }
        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 6px;
            font-size: 14px;
            color: #555;
        }
        .form-group input {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid #dcdfe6;
            border-radius: 6px;
            font-size: 14px;
            outline: none;
            transition: border-color 0.2s;
        }
        .form-group input:focus {
            border-color: #409eff;
        }
        .btn {
            width: 100%;
            padding: 12px;
            background-color: #409eff;
            border: none;
            border-radius: 6px;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .btn:hover { background-color: #337ecc; }
        .tips { text-align: center; margin-top: 15px; font-size: 14px; }
        .tips a { color: #409eff; text-decoration: none; }
        .error-msg { color: #f56c6c; margin-top: 10px; text-align: center; display: none; }
        .success-msg { color: #67c23a; margin-top: 10px; text-align: center; display: none; }
    </style>
</head>
<body>
    <div class="card">
        <h2>注册</h2>
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required />
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required />
            </div>
            <button type="submit" class="btn">注册</button>
            <p id="errorMsg" class="error-msg"></p>
            <p id="successMsg" class="success-msg"></p>
        </form>
        <p class="tips">已有账号？<a href="/login">立即登录</a></p>
    </div>

    <script>
        const form = document.getElementById('registerForm');
        const errorMsg = document.getElementById('errorMsg');
        const successMsg = document.getElementById('successMsg');
        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            const username = form.username.value.trim();
            const password = form.password.value.trim();
            errorMsg.style.display = 'none';
            successMsg.style.display = 'none';
            try {
                const res = await fetch('/api/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password })
                });
                const data = await res.json();
                if (!res.ok) {
                    throw new Error(data.detail || '注册失败');
                }
                successMsg.textContent = '注册成功！3 秒后跳转到登录页面';
                successMsg.style.display = 'block';
                setTimeout(() => {
                    window.location.href = '/login';
                }, 3000);
            } catch (err) {
                errorMsg.textContent = err.message;
                errorMsg.style.display = 'block';
            }
        });
    </script>
</body>
</html> 